package fr.vvins.core.catalog.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import fr.vvins.core.dao.EntityImpl;

@Entity
@Table(schema="vvins",name="VV_CONTENT")
public class Content extends EntityImpl {

	@Id
	@GeneratedValue
	@Column(name="ID")
	private Integer id;
	
	@Basic
	@Column(name="TYPE")
	private String type;
	
	@Basic
	@Column(name="NODE_ID")
	private String nodeId;
	
	@Basic
	@Column(name="KEY")
	private String key;
	
	@OneToMany(mappedBy="content")
	private List<Media> medias;
	
	@Transient
	private Map<String, List<Media>> mediasByType;
	
	@Override
	public Integer getId() {
		return id;
	}

	@Override
	public void setId(Integer id) {
		this.id=id;

	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getNodeId() {
		return nodeId;
	}

	public void setNodeId(String nodeId) {
		this.nodeId = nodeId;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public List<Media> getMedias() {
		return medias;
	}

	public void setMedias(List<Media> medias) {
		this.medias = medias;
	}

	public Map<String, List<Media>> getMediasByType() {
		if(mediasByType == null && getMedias() != null && !getMedias().isEmpty()){
			mediasByType = new HashMap<String, List<Media>>();
			for(Media m : getMedias()){
				List<Media> amediastype =  mediasByType.get(""+m.getMediatype().getId());
				if(amediastype == null){
					amediastype = new ArrayList<Media>();
				}
				amediastype.add(m);
				mediasByType.put(""+m.getMediatype().getId(), amediastype);
			}
		}
		return mediasByType;
	}

	public void setMediasByType(Map<String, List<Media>> mediasByType) {
		this.mediasByType = mediasByType;
	}

}
